{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "\n",
    "def createDataSet():\n",
    "    group=array([[1.1,1.2],[0.99,1.123],[0.11,0.01],[0.0,0.0]])\n",
    "    labels=['A','A','B','B']\n",
    "    return group,labels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.1    1.2  ]\n",
      " [ 0.99   1.123]\n",
      " [ 0.11   0.01 ]\n",
      " [ 0.     0.   ]]\n",
      "['A', 'A', 'B', 'B']\n",
      "4\n",
      "[[ 0.2  0.3]\n",
      " [ 0.2  0.3]\n",
      " [ 0.2  0.3]\n",
      " [ 0.2  0.3]]\n",
      "[[ 0.81      0.81    ]\n",
      " [ 0.6241    0.677329]\n",
      " [ 0.0081    0.0841  ]\n",
      " [ 0.04      0.09    ]]\n",
      "[ 1.62      1.301429  0.0922    0.13    ]\n",
      "[2 3 1 0]\n",
      "3\n",
      "[('C', 23), ('A', 3), ('B', 2)]\n",
      "[('C', 23), ('B', 2), ('A', 3)]\n",
      "[[ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]]\n"
     ]
    }
   ],
   "source": [
    "G,L=createDataSet()\n",
    "print(G)\n",
    "print(L)\n",
    "print(G.shape[0])#行数, shape[1]列数\n",
    "M=tile([0.2,0.3],(4,1))#以[0.2,0.3]为数据，生成4行1列的矩阵\n",
    "print(M)\n",
    "M=M-G\n",
    "print(M**2)#计算矩阵每个元素的平方\n",
    "print((M**2).sum(axis=1))#计算每一行元素的和\n",
    "M=(M**2).sum(axis=1)\n",
    "print(M.argsort())#排序,M[2],M[3],M[1],M[0]\n",
    "D={'A':3,'B':2,\"C\":23}\n",
    "print(D.get('A',0))#得到键'A'对应的值，不存在就返回0\n",
    "sortedD=sorted(D.items(), key=lambda d:d[1], reverse=True)#通过key指定按照值排序，先大\n",
    "print(sortedD)\n",
    "sortedD=sorted(D.items(), key=lambda d:d[0], reverse=True)#通过key指定按照键排序\n",
    "print(sortedD)\n",
    "mt=zeros((10,3))#10行3列的0\n",
    "print(mt)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def classify0(inX, dataSet, labels, k):\n",
    "    dataSetSize = dataSet.shape[0]#shape[0] number of rows, shape[1] number of columns\n",
    "    diffMat=tile(inX,(dataSetSize,1)) - dataSet\n",
    "    sqDiffMat=diffMat**2\n",
    "    sqDist=sqDiffMat.sum(axis=1)\n",
    "    dist=sqDist**0.5\n",
    "    sortedDistIndicies=dist.argsort()\n",
    "    \n",
    "    classCount={}\n",
    "    for i in range(k):\n",
    "        voteIlabel=labels[sortedDistIndicies[i]]\n",
    "        classCount[voteIlabel]=classCount.get(voteIlabel,0)+1\n",
    "    \n",
    "    sortedClassCount=sorted(classCount.items(),key=lambda d:d[1],reverse=True)\n",
    "    return sortedClassCount[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B\n"
     ]
    }
   ],
   "source": [
    "print(classify0([0.21,0.23], G, L, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['40920', '8.326976', '0.953952']\n"
     ]
    }
   ],
   "source": [
    "fr = open('datingTestSet.txt')\n",
    "line=fr.readline()\n",
    "line=line.strip()\n",
    "listFromLine=line.split('\\t')\n",
    "mat=zeros((2,3))\n",
    "mat[0,:]=listFromLine[0:3]\n",
    "print(listFromLine[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  4.09200000e+04   8.32697600e+00   9.53952000e-01]\n",
      " [  1.44880000e+04   7.15346900e+00   1.67390400e+00]\n",
      " [  2.60520000e+04   1.44187100e+00   8.05124000e-01]\n",
      " [  7.51360000e+04   1.31473940e+01   4.28964000e-01]\n",
      " [  3.83440000e+04   1.66978800e+00   1.34296000e-01]\n",
      " [  7.29930000e+04   1.01417400e+01   1.03295500e+00]\n",
      " [  3.59480000e+04   6.83079200e+00   1.21319200e+00]\n",
      " [  4.26660000e+04   1.32763690e+01   5.43880000e-01]\n",
      " [  6.74970000e+04   8.63157700e+00   7.49278000e-01]\n",
      " [  3.54830000e+04   1.22731690e+01   1.50805300e+00]\n",
      " [  5.02420000e+04   3.72349800e+00   8.31917000e-01]\n",
      " [  6.32750000e+04   8.38587900e+00   1.66948500e+00]\n",
      " [  5.56900000e+03   4.87543500e+00   7.28658000e-01]\n",
      " [  5.10520000e+04   4.68009800e+00   6.25224000e-01]\n",
      " [  7.73720000e+04   1.52995700e+01   3.31351000e-01]\n",
      " [  4.36730000e+04   1.88946100e+00   1.91283000e-01]\n",
      " [  6.13640000e+04   7.51675400e+00   1.26916400e+00]\n",
      " [  6.96730000e+04   1.42391950e+01   2.61333000e-01]\n",
      " [  1.56690000e+04   0.00000000e+00   1.25018500e+00]\n",
      " [  2.84880000e+04   1.05285550e+01   1.30484400e+00]\n",
      " [  6.48700000e+03   3.54026500e+00   8.22483000e-01]\n",
      " [  3.77080000e+04   2.99155100e+00   8.33920000e-01]\n",
      " [  2.26200000e+04   5.29786500e+00   6.38306000e-01]\n",
      " [  2.87820000e+04   6.59380300e+00   1.87108000e-01]\n",
      " [  1.97390000e+04   2.81676000e+00   1.68620900e+00]\n",
      " [  3.67880000e+04   1.24582580e+01   6.49617000e-01]\n",
      " [  5.74100000e+03   0.00000000e+00   1.65641800e+00]\n",
      " [  2.85670000e+04   9.96864800e+00   7.31232000e-01]\n",
      " [  6.80800000e+03   1.36483800e+00   6.40103000e-01]\n",
      " [  4.16110000e+04   2.30453000e-01   1.15199600e+00]]\n",
      "['largeDoses', 'smallDoses', 'didntLike', 'didntLike', 'didntLike', 'didntLike', 'largeDoses', 'largeDoses', 'didntLike', 'largeDoses', 'didntLike', 'didntLike', 'smallDoses', 'didntLike', 'didntLike', 'didntLike', 'didntLike', 'didntLike', 'smallDoses', 'largeDoses']\n"
     ]
    }
   ],
   "source": [
    "def file2matrix(filename):\n",
    "    fr = open(filename)\n",
    "    numberOfLines=len(fr.readlines())\n",
    "    retMat=zeros((numberOfLines,3))\n",
    "    classLabelVector=[]\n",
    "    fr = open(filename,'r',encoding='utf-8')\n",
    "    for i,line in enumerate(fr.readlines()):\n",
    "        line=line.strip()\n",
    "        listFromLine=line.split('\\t')\n",
    "        retMat[i,:]=listFromLine[0:3]\n",
    "        classLabelVector.append((listFromLine[-1]))\n",
    "    return retMat,classLabelVector\n",
    "\n",
    "datingDataMat,datingLabels=file2matrix('datingTestSet.txt')\n",
    "print(datingDataMat[0:30,:])\n",
    "print(datingLabels[0:20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "mat=[[1,2,3],\n",
    "    [4,5,6]]\n",
    "mat=array(mat)#转化为矩阵才可以用min,max\n",
    "minVals=mat.min(0)#得到每一列的最小值, min(1)得到每一行的最小值\n",
    "print(minVals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.44832535,  0.39805139,  0.56233353],\n",
       "       [ 0.15873259,  0.34195467,  0.98724416],\n",
       "       [ 0.28542943,  0.06892523,  0.47449629],\n",
       "       ..., \n",
       "       [ 0.29115949,  0.50910294,  0.51079493],\n",
       "       [ 0.52711097,  0.43665451,  0.4290048 ],\n",
       "       [ 0.47940793,  0.3768091 ,  0.78571804]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def autoNorm(dataSet):\n",
    "    minVals = dataSet.min(0)\n",
    "    maxVals = dataSet.max(0)\n",
    "    ranges=maxVals-minVals\n",
    "    mormDataSet=zeros(shape(dataSet))\n",
    "    m=dataSet.shape[0]\n",
    "    normDataSet=dataSet-tile(minVals,(m,1))\n",
    "    normDataSet=normDataSet/tile(ranges,(m,1))\n",
    "    return normDataSet,ranges,minVals\n",
    "normMat,ranges,minVals=autoNorm(datingDataMat)\n",
    "normMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  9.12730000e+04,   2.09193490e+01,   1.69436100e+00])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ranges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.      ,  0.      ,  0.001156])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "minVals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0th test result largeDoses, the real answer is largeDoses\n",
      "1th test result smallDoses, the real answer is smallDoses\n",
      "2th test result didntLike, the real answer is didntLike\n",
      "3th test result didntLike, the real answer is didntLike\n",
      "4th test result didntLike, the real answer is didntLike\n",
      "5th test result didntLike, the real answer is didntLike\n",
      "6th test result largeDoses, the real answer is largeDoses\n",
      "7th test result largeDoses, the real answer is largeDoses\n",
      "8th test result didntLike, the real answer is didntLike\n",
      "9th test result largeDoses, the real answer is largeDoses\n",
      "10th test result didntLike, the real answer is didntLike\n",
      "11th test result didntLike, the real answer is didntLike\n",
      "12th test result smallDoses, the real answer is smallDoses\n",
      "13th test result didntLike, the real answer is didntLike\n",
      "14th test result didntLike, the real answer is didntLike\n",
      "15th test result didntLike, the real answer is didntLike\n",
      "16th test result didntLike, the real answer is didntLike\n",
      "17th test result didntLike, the real answer is didntLike\n",
      "18th test result smallDoses, the real answer is smallDoses\n",
      "19th test result largeDoses, the real answer is largeDoses\n",
      "20th test result smallDoses, the real answer is smallDoses\n",
      "21th test result didntLike, the real answer is didntLike\n",
      "22th test result largeDoses, the real answer is smallDoses\n",
      "23th test result largeDoses, the real answer is largeDoses\n",
      "24th test result smallDoses, the real answer is smallDoses\n",
      "25th test result largeDoses, the real answer is largeDoses\n",
      "26th test result smallDoses, the real answer is smallDoses\n",
      "27th test result largeDoses, the real answer is largeDoses\n",
      "28th test result smallDoses, the real answer is smallDoses\n",
      "29th test result didntLike, the real answer is didntLike\n",
      "30th test result largeDoses, the real answer is largeDoses\n",
      "31th test result didntLike, the real answer is didntLike\n",
      "32th test result largeDoses, the real answer is largeDoses\n",
      "33th test result didntLike, the real answer is didntLike\n",
      "34th test result smallDoses, the real answer is smallDoses\n",
      "35th test result didntLike, the real answer is didntLike\n",
      "36th test result didntLike, the real answer is didntLike\n",
      "37th test result smallDoses, the real answer is smallDoses\n",
      "38th test result largeDoses, the real answer is largeDoses\n",
      "39th test result largeDoses, the real answer is largeDoses\n",
      "40th test result didntLike, the real answer is didntLike\n",
      "41th test result smallDoses, the real answer is smallDoses\n",
      "42th test result largeDoses, the real answer is largeDoses\n",
      "43th test result largeDoses, the real answer is largeDoses\n",
      "44th test result largeDoses, the real answer is largeDoses\n",
      "45th test result didntLike, the real answer is didntLike\n",
      "46th test result didntLike, the real answer is didntLike\n",
      "47th test result didntLike, the real answer is didntLike\n",
      "48th test result didntLike, the real answer is didntLike\n",
      "49th test result smallDoses, the real answer is smallDoses\n",
      "50th test result smallDoses, the real answer is smallDoses\n",
      "51th test result didntLike, the real answer is didntLike\n",
      "52th test result largeDoses, the real answer is largeDoses\n",
      "53th test result smallDoses, the real answer is smallDoses\n",
      "54th test result smallDoses, the real answer is smallDoses\n",
      "55th test result smallDoses, the real answer is smallDoses\n",
      "56th test result smallDoses, the real answer is smallDoses\n",
      "57th test result largeDoses, the real answer is largeDoses\n",
      "58th test result didntLike, the real answer is didntLike\n",
      "59th test result smallDoses, the real answer is smallDoses\n",
      "60th test result didntLike, the real answer is didntLike\n",
      "61th test result smallDoses, the real answer is smallDoses\n",
      "62th test result smallDoses, the real answer is smallDoses\n",
      "63th test result smallDoses, the real answer is smallDoses\n",
      "64th test result smallDoses, the real answer is smallDoses\n",
      "65th test result smallDoses, the real answer is smallDoses\n",
      "66th test result largeDoses, the real answer is largeDoses\n",
      "67th test result smallDoses, the real answer is smallDoses\n",
      "68th test result largeDoses, the real answer is largeDoses\n",
      "69th test result didntLike, the real answer is didntLike\n",
      "70th test result smallDoses, the real answer is smallDoses\n",
      "71th test result largeDoses, the real answer is largeDoses\n",
      "72th test result smallDoses, the real answer is smallDoses\n",
      "73th test result smallDoses, the real answer is smallDoses\n",
      "74th test result largeDoses, the real answer is didntLike\n",
      "75th test result largeDoses, the real answer is largeDoses\n",
      "76th test result didntLike, the real answer is didntLike\n",
      "77th test result didntLike, the real answer is didntLike\n",
      "78th test result largeDoses, the real answer is largeDoses\n",
      "79th test result largeDoses, the real answer is largeDoses\n",
      "80th test result didntLike, the real answer is didntLike\n",
      "81th test result smallDoses, the real answer is smallDoses\n",
      "82th test result largeDoses, the real answer is largeDoses\n",
      "83th test result largeDoses, the real answer is didntLike\n",
      "84th test result largeDoses, the real answer is largeDoses\n",
      "85th test result didntLike, the real answer is didntLike\n",
      "86th test result smallDoses, the real answer is smallDoses\n",
      "87th test result smallDoses, the real answer is smallDoses\n",
      "88th test result didntLike, the real answer is didntLike\n",
      "89th test result didntLike, the real answer is didntLike\n",
      "90th test result largeDoses, the real answer is largeDoses\n",
      "91th test result smallDoses, the real answer is largeDoses\n",
      "92th test result didntLike, the real answer is didntLike\n",
      "93th test result smallDoses, the real answer is smallDoses\n",
      "94th test result didntLike, the real answer is didntLike\n",
      "95th test result largeDoses, the real answer is largeDoses\n",
      "96th test result largeDoses, the real answer is largeDoses\n",
      "97th test result smallDoses, the real answer is smallDoses\n",
      "98th test result didntLike, the real answer is didntLike\n",
      "99th test result largeDoses, the real answer is didntLike\n",
      "the total error rate is 0.050000\n"
     ]
    }
   ],
   "source": [
    "def datingTest():\n",
    "    hoRatio=0.10\n",
    "    datingDataMat,datingLabels=file2matrix('datingTestSet.txt')\n",
    "    normMat, ranges, minVals=autoNorm(datingDataMat)\n",
    "    m=normMat.shape[0]\n",
    "    numTestVecs=int(m*hoRatio)\n",
    "    errorCount=0.0\n",
    "    for i in range(numTestVecs):\n",
    "        classifierResult=classify0(normMat[i,:],normMat[numTestVecs:m,:],\\\n",
    "                                 datingLabels[numTestVecs:m],3)\n",
    "        print(\"%dth test result %s, the real answer is %s\" %\\\n",
    "              (i,classifierResult,datingLabels[i]))\n",
    "        if(classifierResult!=datingLabels[i]):\n",
    "            errorCount+=1.0\n",
    "    print (\"the total error rate is %f\" % (errorCount/float(numTestVecs)))\n",
    "datingTest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "percentage of time spent playing video games? 10\n",
      "frequent flier miles earned per years? 10000\n",
      "liters of ice cream consumed per years? 0.5\n",
      "smallDoses\n"
     ]
    }
   ],
   "source": [
    "def classifyPerson():\n",
    "    percentT=float(input(\\\n",
    "                            \"percentage of time spent playing video games? \"))\n",
    "    ffMiles=float(input(\\\n",
    "                           \"frequent flier miles earned per years? \"))\n",
    "    iceCream=float(input(\\\n",
    "                            \"liters of ice cream consumed per years? \"))\n",
    "    datingDataMat,datingLabels=file2matrix('datingTestSet.txt')\n",
    "    normMat,ranges,minVals=autoNorm(datingDataMat)\n",
    "    inArr=array([ffMiles,percentT,iceCream])\n",
    "    result=classify0((inArr-minVals)/ranges,\\\n",
    "                    normMat,datingLabels,3)\n",
    "    print(result)\n",
    "classifyPerson()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 0.,  0.])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=zeros((2,4))\n",
    "print(a[0,0])#表示第一行第一列的元素\n",
    "print(a[0])#表示第一行的元素\n",
    "a[:,0]#表示第一列的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,\n",
       "        0.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,\n",
       "        0.,  0.,  0.,  0.])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def img2vec(filename):\n",
    "    retVec=zeros((1,1024))\n",
    "    fr=open(filename,'r',encoding='utf-8')\n",
    "    for i in range(32):\n",
    "        lineStr=fr.readline()\n",
    "        for j in range(32):\n",
    "            retVec[0,32*i+j]=int(lineStr[j])\n",
    "    return retVec\n",
    "testVec=img2vec('testDigits/0_13.txt')\n",
    "testVec[0,0:30]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0th test result 0, the real answer is 0\n",
      "1th test result 0, the real answer is 0\n",
      "2th test result 0, the real answer is 0\n",
      "3th test result 0, the real answer is 0\n",
      "4th test result 0, the real answer is 0\n",
      "5th test result 0, the real answer is 0\n",
      "6th test result 0, the real answer is 0\n",
      "7th test result 0, the real answer is 0\n",
      "8th test result 0, the real answer is 0\n",
      "9th test result 0, the real answer is 0\n",
      "10th test result 0, the real answer is 0\n",
      "11th test result 0, the real answer is 0\n",
      "12th test result 0, the real answer is 0\n",
      "13th test result 0, the real answer is 0\n",
      "14th test result 0, the real answer is 0\n",
      "15th test result 0, the real answer is 0\n",
      "16th test result 0, the real answer is 0\n",
      "17th test result 0, the real answer is 0\n",
      "18th test result 0, the real answer is 0\n",
      "19th test result 0, the real answer is 0\n",
      "20th test result 0, the real answer is 0\n",
      "21th test result 0, the real answer is 0\n",
      "22th test result 0, the real answer is 0\n",
      "23th test result 0, the real answer is 0\n",
      "24th test result 0, the real answer is 0\n",
      "25th test result 0, the real answer is 0\n",
      "26th test result 0, the real answer is 0\n",
      "27th test result 0, the real answer is 0\n",
      "28th test result 0, the real answer is 0\n",
      "29th test result 0, the real answer is 0\n",
      "30th test result 0, the real answer is 0\n",
      "31th test result 0, the real answer is 0\n",
      "32th test result 0, the real answer is 0\n",
      "33th test result 0, the real answer is 0\n",
      "34th test result 0, the real answer is 0\n",
      "35th test result 0, the real answer is 0\n",
      "36th test result 0, the real answer is 0\n",
      "37th test result 0, the real answer is 0\n",
      "38th test result 0, the real answer is 0\n",
      "39th test result 0, the real answer is 0\n",
      "40th test result 0, the real answer is 0\n",
      "41th test result 0, the real answer is 0\n",
      "42th test result 0, the real answer is 0\n",
      "43th test result 0, the real answer is 0\n",
      "44th test result 0, the real answer is 0\n",
      "45th test result 0, the real answer is 0\n",
      "46th test result 0, the real answer is 0\n",
      "47th test result 0, the real answer is 0\n",
      "48th test result 0, the real answer is 0\n",
      "49th test result 0, the real answer is 0\n",
      "50th test result 0, the real answer is 0\n",
      "51th test result 0, the real answer is 0\n",
      "52th test result 0, the real answer is 0\n",
      "53th test result 0, the real answer is 0\n",
      "54th test result 0, the real answer is 0\n",
      "55th test result 0, the real answer is 0\n",
      "56th test result 0, the real answer is 0\n",
      "57th test result 0, the real answer is 0\n",
      "58th test result 0, the real answer is 0\n",
      "59th test result 0, the real answer is 0\n",
      "60th test result 0, the real answer is 0\n",
      "61th test result 0, the real answer is 0\n",
      "62th test result 0, the real answer is 0\n",
      "63th test result 0, the real answer is 0\n",
      "64th test result 0, the real answer is 0\n",
      "65th test result 0, the real answer is 0\n",
      "66th test result 0, the real answer is 0\n",
      "67th test result 0, the real answer is 0\n",
      "68th test result 0, the real answer is 0\n",
      "69th test result 0, the real answer is 0\n",
      "70th test result 0, the real answer is 0\n",
      "71th test result 0, the real answer is 0\n",
      "72th test result 0, the real answer is 0\n",
      "73th test result 0, the real answer is 0\n",
      "74th test result 0, the real answer is 0\n",
      "75th test result 0, the real answer is 0\n",
      "76th test result 0, the real answer is 0\n",
      "77th test result 0, the real answer is 0\n",
      "78th test result 0, the real answer is 0\n",
      "79th test result 0, the real answer is 0\n",
      "80th test result 0, the real answer is 0\n",
      "81th test result 0, the real answer is 0\n",
      "82th test result 0, the real answer is 0\n",
      "83th test result 0, the real answer is 0\n",
      "84th test result 0, the real answer is 0\n",
      "85th test result 0, the real answer is 0\n",
      "86th test result 0, the real answer is 0\n",
      "87th test result 1, the real answer is 1\n",
      "88th test result 1, the real answer is 1\n",
      "89th test result 1, the real answer is 1\n",
      "90th test result 1, the real answer is 1\n",
      "91th test result 1, the real answer is 1\n",
      "92th test result 1, the real answer is 1\n",
      "93th test result 1, the real answer is 1\n",
      "94th test result 1, the real answer is 1\n",
      "95th test result 1, the real answer is 1\n",
      "96th test result 1, the real answer is 1\n",
      "97th test result 1, the real answer is 1\n",
      "98th test result 1, the real answer is 1\n",
      "99th test result 1, the real answer is 1\n",
      "100th test result 1, the real answer is 1\n",
      "101th test result 1, the real answer is 1\n",
      "102th test result 1, the real answer is 1\n",
      "103th test result 1, the real answer is 1\n",
      "104th test result 1, the real answer is 1\n",
      "105th test result 1, the real answer is 1\n",
      "106th test result 1, the real answer is 1\n",
      "107th test result 1, the real answer is 1\n",
      "108th test result 1, the real answer is 1\n",
      "109th test result 1, the real answer is 1\n",
      "110th test result 1, the real answer is 1\n",
      "111th test result 1, the real answer is 1\n",
      "112th test result 1, the real answer is 1\n",
      "113th test result 1, the real answer is 1\n",
      "114th test result 1, the real answer is 1\n",
      "115th test result 1, the real answer is 1\n",
      "116th test result 1, the real answer is 1\n",
      "117th test result 1, the real answer is 1\n",
      "118th test result 1, the real answer is 1\n",
      "119th test result 1, the real answer is 1\n",
      "120th test result 1, the real answer is 1\n",
      "121th test result 1, the real answer is 1\n",
      "122th test result 1, the real answer is 1\n",
      "123th test result 1, the real answer is 1\n",
      "124th test result 1, the real answer is 1\n",
      "125th test result 1, the real answer is 1\n",
      "126th test result 1, the real answer is 1\n",
      "127th test result 1, the real answer is 1\n",
      "128th test result 1, the real answer is 1\n",
      "129th test result 1, the real answer is 1\n",
      "130th test result 1, the real answer is 1\n",
      "131th test result 1, the real answer is 1\n",
      "132th test result 1, the real answer is 1\n",
      "133th test result 1, the real answer is 1\n",
      "134th test result 1, the real answer is 1\n",
      "135th test result 1, the real answer is 1\n",
      "136th test result 1, the real answer is 1\n",
      "137th test result 1, the real answer is 1\n",
      "138th test result 1, the real answer is 1\n",
      "139th test result 1, the real answer is 1\n",
      "140th test result 1, the real answer is 1\n",
      "141th test result 1, the real answer is 1\n",
      "142th test result 1, the real answer is 1\n",
      "143th test result 1, the real answer is 1\n",
      "144th test result 1, the real answer is 1\n",
      "145th test result 1, the real answer is 1\n",
      "146th test result 1, the real answer is 1\n",
      "147th test result 1, the real answer is 1\n",
      "148th test result 1, the real answer is 1\n",
      "149th test result 1, the real answer is 1\n",
      "150th test result 1, the real answer is 1\n",
      "151th test result 1, the real answer is 1\n",
      "152th test result 1, the real answer is 1\n",
      "153th test result 1, the real answer is 1\n",
      "154th test result 1, the real answer is 1\n",
      "155th test result 1, the real answer is 1\n",
      "156th test result 1, the real answer is 1\n",
      "157th test result 1, the real answer is 1\n",
      "158th test result 1, the real answer is 1\n",
      "159th test result 1, the real answer is 1\n",
      "160th test result 1, the real answer is 1\n",
      "161th test result 1, the real answer is 1\n",
      "162th test result 1, the real answer is 1\n",
      "163th test result 1, the real answer is 1\n",
      "164th test result 1, the real answer is 1\n",
      "165th test result 1, the real answer is 1\n",
      "166th test result 1, the real answer is 1\n",
      "167th test result 1, the real answer is 1\n",
      "168th test result 1, the real answer is 1\n",
      "169th test result 1, the real answer is 1\n",
      "170th test result 1, the real answer is 1\n",
      "171th test result 1, the real answer is 1\n",
      "172th test result 7, the real answer is 1\n",
      "173th test result 1, the real answer is 1\n",
      "174th test result 1, the real answer is 1\n",
      "175th test result 1, the real answer is 1\n",
      "176th test result 1, the real answer is 1\n",
      "177th test result 1, the real answer is 1\n",
      "178th test result 1, the real answer is 1\n",
      "179th test result 1, the real answer is 1\n",
      "180th test result 1, the real answer is 1\n",
      "181th test result 1, the real answer is 1\n",
      "182th test result 1, the real answer is 1\n",
      "183th test result 1, the real answer is 1\n",
      "184th test result 2, the real answer is 2\n",
      "185th test result 2, the real answer is 2\n",
      "186th test result 2, the real answer is 2\n",
      "187th test result 2, the real answer is 2\n",
      "188th test result 2, the real answer is 2\n",
      "189th test result 2, the real answer is 2\n",
      "190th test result 2, the real answer is 2\n",
      "191th test result 2, the real answer is 2\n",
      "192th test result 2, the real answer is 2\n",
      "193th test result 2, the real answer is 2\n",
      "194th test result 2, the real answer is 2\n",
      "195th test result 2, the real answer is 2\n",
      "196th test result 2, the real answer is 2\n",
      "197th test result 2, the real answer is 2\n",
      "198th test result 2, the real answer is 2\n",
      "199th test result 2, the real answer is 2\n",
      "200th test result 2, the real answer is 2\n",
      "201th test result 2, the real answer is 2\n",
      "202th test result 2, the real answer is 2\n",
      "203th test result 2, the real answer is 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "204th test result 2, the real answer is 2\n",
      "205th test result 2, the real answer is 2\n",
      "206th test result 2, the real answer is 2\n",
      "207th test result 2, the real answer is 2\n",
      "208th test result 2, the real answer is 2\n",
      "209th test result 2, the real answer is 2\n",
      "210th test result 2, the real answer is 2\n",
      "211th test result 2, the real answer is 2\n",
      "212th test result 2, the real answer is 2\n",
      "213th test result 2, the real answer is 2\n",
      "214th test result 2, the real answer is 2\n",
      "215th test result 2, the real answer is 2\n",
      "216th test result 2, the real answer is 2\n",
      "217th test result 2, the real answer is 2\n",
      "218th test result 2, the real answer is 2\n",
      "219th test result 2, the real answer is 2\n",
      "220th test result 2, the real answer is 2\n",
      "221th test result 2, the real answer is 2\n",
      "222th test result 2, the real answer is 2\n",
      "223th test result 2, the real answer is 2\n",
      "224th test result 2, the real answer is 2\n",
      "225th test result 2, the real answer is 2\n",
      "226th test result 2, the real answer is 2\n",
      "227th test result 2, the real answer is 2\n",
      "228th test result 2, the real answer is 2\n",
      "229th test result 2, the real answer is 2\n",
      "230th test result 2, the real answer is 2\n",
      "231th test result 2, the real answer is 2\n",
      "232th test result 2, the real answer is 2\n",
      "233th test result 2, the real answer is 2\n",
      "234th test result 2, the real answer is 2\n",
      "235th test result 2, the real answer is 2\n",
      "236th test result 2, the real answer is 2\n",
      "237th test result 2, the real answer is 2\n",
      "238th test result 2, the real answer is 2\n",
      "239th test result 2, the real answer is 2\n",
      "240th test result 2, the real answer is 2\n",
      "241th test result 2, the real answer is 2\n",
      "242th test result 2, the real answer is 2\n",
      "243th test result 2, the real answer is 2\n",
      "244th test result 2, the real answer is 2\n",
      "245th test result 2, the real answer is 2\n",
      "246th test result 2, the real answer is 2\n",
      "247th test result 2, the real answer is 2\n",
      "248th test result 2, the real answer is 2\n",
      "249th test result 2, the real answer is 2\n",
      "250th test result 2, the real answer is 2\n",
      "251th test result 2, the real answer is 2\n",
      "252th test result 2, the real answer is 2\n",
      "253th test result 2, the real answer is 2\n",
      "254th test result 2, the real answer is 2\n",
      "255th test result 2, the real answer is 2\n",
      "256th test result 2, the real answer is 2\n",
      "257th test result 2, the real answer is 2\n",
      "258th test result 2, the real answer is 2\n",
      "259th test result 2, the real answer is 2\n",
      "260th test result 2, the real answer is 2\n",
      "261th test result 2, the real answer is 2\n",
      "262th test result 2, the real answer is 2\n",
      "263th test result 2, the real answer is 2\n",
      "264th test result 2, the real answer is 2\n",
      "265th test result 2, the real answer is 2\n",
      "266th test result 2, the real answer is 2\n",
      "267th test result 2, the real answer is 2\n",
      "268th test result 2, the real answer is 2\n",
      "269th test result 2, the real answer is 2\n",
      "270th test result 2, the real answer is 2\n",
      "271th test result 2, the real answer is 2\n",
      "272th test result 2, the real answer is 2\n",
      "273th test result 2, the real answer is 2\n",
      "274th test result 2, the real answer is 2\n",
      "275th test result 2, the real answer is 2\n",
      "276th test result 3, the real answer is 3\n",
      "277th test result 3, the real answer is 3\n",
      "278th test result 3, the real answer is 3\n",
      "279th test result 9, the real answer is 3\n",
      "280th test result 3, the real answer is 3\n",
      "281th test result 3, the real answer is 3\n",
      "282th test result 3, the real answer is 3\n",
      "283th test result 3, the real answer is 3\n",
      "284th test result 3, the real answer is 3\n",
      "285th test result 3, the real answer is 3\n",
      "286th test result 3, the real answer is 3\n",
      "287th test result 3, the real answer is 3\n",
      "288th test result 3, the real answer is 3\n",
      "289th test result 3, the real answer is 3\n",
      "290th test result 3, the real answer is 3\n",
      "291th test result 3, the real answer is 3\n",
      "292th test result 3, the real answer is 3\n",
      "293th test result 3, the real answer is 3\n",
      "294th test result 3, the real answer is 3\n",
      "295th test result 3, the real answer is 3\n",
      "296th test result 3, the real answer is 3\n",
      "297th test result 3, the real answer is 3\n",
      "298th test result 3, the real answer is 3\n",
      "299th test result 3, the real answer is 3\n",
      "300th test result 3, the real answer is 3\n",
      "301th test result 3, the real answer is 3\n",
      "302th test result 3, the real answer is 3\n",
      "303th test result 3, the real answer is 3\n",
      "304th test result 3, the real answer is 3\n",
      "305th test result 3, the real answer is 3\n",
      "306th test result 3, the real answer is 3\n",
      "307th test result 3, the real answer is 3\n",
      "308th test result 3, the real answer is 3\n",
      "309th test result 3, the real answer is 3\n",
      "310th test result 3, the real answer is 3\n",
      "311th test result 3, the real answer is 3\n",
      "312th test result 3, the real answer is 3\n",
      "313th test result 3, the real answer is 3\n",
      "314th test result 3, the real answer is 3\n",
      "315th test result 3, the real answer is 3\n",
      "316th test result 3, the real answer is 3\n",
      "317th test result 3, the real answer is 3\n",
      "318th test result 3, the real answer is 3\n",
      "319th test result 3, the real answer is 3\n",
      "320th test result 3, the real answer is 3\n",
      "321th test result 3, the real answer is 3\n",
      "322th test result 3, the real answer is 3\n",
      "323th test result 3, the real answer is 3\n",
      "324th test result 3, the real answer is 3\n",
      "325th test result 3, the real answer is 3\n",
      "326th test result 3, the real answer is 3\n",
      "327th test result 3, the real answer is 3\n",
      "328th test result 3, the real answer is 3\n",
      "329th test result 3, the real answer is 3\n",
      "330th test result 3, the real answer is 3\n",
      "331th test result 3, the real answer is 3\n",
      "332th test result 3, the real answer is 3\n",
      "333th test result 3, the real answer is 3\n",
      "334th test result 3, the real answer is 3\n",
      "335th test result 3, the real answer is 3\n",
      "336th test result 3, the real answer is 3\n",
      "337th test result 3, the real answer is 3\n",
      "338th test result 3, the real answer is 3\n",
      "339th test result 3, the real answer is 3\n",
      "340th test result 3, the real answer is 3\n",
      "341th test result 3, the real answer is 3\n",
      "342th test result 3, the real answer is 3\n",
      "343th test result 3, the real answer is 3\n",
      "344th test result 3, the real answer is 3\n",
      "345th test result 3, the real answer is 3\n",
      "346th test result 3, the real answer is 3\n",
      "347th test result 3, the real answer is 3\n",
      "348th test result 3, the real answer is 3\n",
      "349th test result 3, the real answer is 3\n",
      "350th test result 3, the real answer is 3\n",
      "351th test result 3, the real answer is 3\n",
      "352th test result 3, the real answer is 3\n",
      "353th test result 3, the real answer is 3\n",
      "354th test result 3, the real answer is 3\n",
      "355th test result 3, the real answer is 3\n",
      "356th test result 3, the real answer is 3\n",
      "357th test result 3, the real answer is 3\n",
      "358th test result 3, the real answer is 3\n",
      "359th test result 3, the real answer is 3\n",
      "360th test result 3, the real answer is 3\n",
      "361th test result 4, the real answer is 4\n",
      "362th test result 4, the real answer is 4\n",
      "363th test result 4, the real answer is 4\n",
      "364th test result 4, the real answer is 4\n",
      "365th test result 4, the real answer is 4\n",
      "366th test result 4, the real answer is 4\n",
      "367th test result 4, the real answer is 4\n",
      "368th test result 4, the real answer is 4\n",
      "369th test result 4, the real answer is 4\n",
      "370th test result 4, the real answer is 4\n",
      "371th test result 4, the real answer is 4\n",
      "372th test result 4, the real answer is 4\n",
      "373th test result 4, the real answer is 4\n",
      "374th test result 4, the real answer is 4\n",
      "375th test result 4, the real answer is 4\n",
      "376th test result 4, the real answer is 4\n",
      "377th test result 4, the real answer is 4\n",
      "378th test result 4, the real answer is 4\n",
      "379th test result 4, the real answer is 4\n",
      "380th test result 4, the real answer is 4\n",
      "381th test result 4, the real answer is 4\n",
      "382th test result 4, the real answer is 4\n",
      "383th test result 4, the real answer is 4\n",
      "384th test result 4, the real answer is 4\n",
      "385th test result 4, the real answer is 4\n",
      "386th test result 4, the real answer is 4\n",
      "387th test result 4, the real answer is 4\n",
      "388th test result 4, the real answer is 4\n",
      "389th test result 4, the real answer is 4\n",
      "390th test result 4, the real answer is 4\n",
      "391th test result 4, the real answer is 4\n",
      "392th test result 4, the real answer is 4\n",
      "393th test result 4, the real answer is 4\n",
      "394th test result 4, the real answer is 4\n",
      "395th test result 4, the real answer is 4\n",
      "396th test result 4, the real answer is 4\n",
      "397th test result 4, the real answer is 4\n",
      "398th test result 4, the real answer is 4\n",
      "399th test result 4, the real answer is 4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "400th test result 4, the real answer is 4\n",
      "401th test result 4, the real answer is 4\n",
      "402th test result 4, the real answer is 4\n",
      "403th test result 4, the real answer is 4\n",
      "404th test result 4, the real answer is 4\n",
      "405th test result 4, the real answer is 4\n",
      "406th test result 4, the real answer is 4\n",
      "407th test result 4, the real answer is 4\n",
      "408th test result 4, the real answer is 4\n",
      "409th test result 4, the real answer is 4\n",
      "410th test result 4, the real answer is 4\n",
      "411th test result 4, the real answer is 4\n",
      "412th test result 4, the real answer is 4\n",
      "413th test result 4, the real answer is 4\n",
      "414th test result 4, the real answer is 4\n",
      "415th test result 4, the real answer is 4\n",
      "416th test result 4, the real answer is 4\n",
      "417th test result 4, the real answer is 4\n",
      "418th test result 4, the real answer is 4\n",
      "419th test result 4, the real answer is 4\n",
      "420th test result 4, the real answer is 4\n",
      "421th test result 4, the real answer is 4\n",
      "422th test result 4, the real answer is 4\n",
      "423th test result 4, the real answer is 4\n",
      "424th test result 4, the real answer is 4\n",
      "425th test result 4, the real answer is 4\n",
      "426th test result 4, the real answer is 4\n",
      "427th test result 4, the real answer is 4\n",
      "428th test result 4, the real answer is 4\n",
      "429th test result 4, the real answer is 4\n",
      "430th test result 4, the real answer is 4\n",
      "431th test result 4, the real answer is 4\n",
      "432th test result 4, the real answer is 4\n",
      "433th test result 4, the real answer is 4\n",
      "434th test result 4, the real answer is 4\n",
      "435th test result 4, the real answer is 4\n",
      "436th test result 4, the real answer is 4\n",
      "437th test result 4, the real answer is 4\n",
      "438th test result 4, the real answer is 4\n",
      "439th test result 4, the real answer is 4\n",
      "440th test result 4, the real answer is 4\n",
      "441th test result 4, the real answer is 4\n",
      "442th test result 4, the real answer is 4\n",
      "443th test result 4, the real answer is 4\n",
      "444th test result 4, the real answer is 4\n",
      "445th test result 4, the real answer is 4\n",
      "446th test result 4, the real answer is 4\n",
      "447th test result 4, the real answer is 4\n",
      "448th test result 4, the real answer is 4\n",
      "449th test result 4, the real answer is 4\n",
      "450th test result 4, the real answer is 4\n",
      "451th test result 4, the real answer is 4\n",
      "452th test result 4, the real answer is 4\n",
      "453th test result 4, the real answer is 4\n",
      "454th test result 4, the real answer is 4\n",
      "455th test result 4, the real answer is 4\n",
      "456th test result 4, the real answer is 4\n",
      "457th test result 4, the real answer is 4\n",
      "458th test result 4, the real answer is 4\n",
      "459th test result 4, the real answer is 4\n",
      "460th test result 4, the real answer is 4\n",
      "461th test result 4, the real answer is 4\n",
      "462th test result 4, the real answer is 4\n",
      "463th test result 4, the real answer is 4\n",
      "464th test result 4, the real answer is 4\n",
      "465th test result 4, the real answer is 4\n",
      "466th test result 4, the real answer is 4\n",
      "467th test result 4, the real answer is 4\n",
      "468th test result 4, the real answer is 4\n",
      "469th test result 4, the real answer is 4\n",
      "470th test result 4, the real answer is 4\n",
      "471th test result 4, the real answer is 4\n",
      "472th test result 4, the real answer is 4\n",
      "473th test result 4, the real answer is 4\n",
      "474th test result 4, the real answer is 4\n",
      "475th test result 5, the real answer is 5\n",
      "476th test result 5, the real answer is 5\n",
      "477th test result 5, the real answer is 5\n",
      "478th test result 5, the real answer is 5\n",
      "479th test result 5, the real answer is 5\n",
      "480th test result 5, the real answer is 5\n",
      "481th test result 5, the real answer is 5\n",
      "482th test result 5, the real answer is 5\n",
      "483th test result 5, the real answer is 5\n",
      "484th test result 5, the real answer is 5\n",
      "485th test result 5, the real answer is 5\n",
      "486th test result 5, the real answer is 5\n",
      "487th test result 5, the real answer is 5\n",
      "488th test result 5, the real answer is 5\n",
      "489th test result 5, the real answer is 5\n",
      "490th test result 5, the real answer is 5\n",
      "491th test result 5, the real answer is 5\n",
      "492th test result 5, the real answer is 5\n",
      "493th test result 5, the real answer is 5\n",
      "494th test result 5, the real answer is 5\n",
      "495th test result 5, the real answer is 5\n",
      "496th test result 5, the real answer is 5\n",
      "497th test result 5, the real answer is 5\n",
      "498th test result 5, the real answer is 5\n",
      "499th test result 5, the real answer is 5\n",
      "500th test result 5, the real answer is 5\n",
      "501th test result 5, the real answer is 5\n",
      "502th test result 5, the real answer is 5\n",
      "503th test result 5, the real answer is 5\n",
      "504th test result 5, the real answer is 5\n",
      "505th test result 5, the real answer is 5\n",
      "506th test result 5, the real answer is 5\n",
      "507th test result 5, the real answer is 5\n",
      "508th test result 5, the real answer is 5\n",
      "509th test result 5, the real answer is 5\n",
      "510th test result 5, the real answer is 5\n",
      "511th test result 5, the real answer is 5\n",
      "512th test result 5, the real answer is 5\n",
      "513th test result 5, the real answer is 5\n",
      "514th test result 5, the real answer is 5\n",
      "515th test result 5, the real answer is 5\n",
      "516th test result 5, the real answer is 5\n",
      "517th test result 5, the real answer is 5\n",
      "518th test result 5, the real answer is 5\n",
      "519th test result 5, the real answer is 5\n",
      "520th test result 3, the real answer is 5\n",
      "521th test result 6, the real answer is 5\n",
      "522th test result 5, the real answer is 5\n",
      "523th test result 5, the real answer is 5\n",
      "524th test result 5, the real answer is 5\n",
      "525th test result 5, the real answer is 5\n",
      "526th test result 5, the real answer is 5\n",
      "527th test result 5, the real answer is 5\n",
      "528th test result 5, the real answer is 5\n",
      "529th test result 5, the real answer is 5\n",
      "530th test result 5, the real answer is 5\n",
      "531th test result 5, the real answer is 5\n",
      "532th test result 5, the real answer is 5\n",
      "533th test result 5, the real answer is 5\n",
      "534th test result 5, the real answer is 5\n",
      "535th test result 5, the real answer is 5\n",
      "536th test result 5, the real answer is 5\n",
      "537th test result 5, the real answer is 5\n",
      "538th test result 5, the real answer is 5\n",
      "539th test result 5, the real answer is 5\n",
      "540th test result 5, the real answer is 5\n",
      "541th test result 5, the real answer is 5\n",
      "542th test result 5, the real answer is 5\n",
      "543th test result 5, the real answer is 5\n",
      "544th test result 5, the real answer is 5\n",
      "545th test result 5, the real answer is 5\n",
      "546th test result 5, the real answer is 5\n",
      "547th test result 5, the real answer is 5\n",
      "548th test result 5, the real answer is 5\n",
      "549th test result 5, the real answer is 5\n",
      "550th test result 5, the real answer is 5\n",
      "551th test result 5, the real answer is 5\n",
      "552th test result 5, the real answer is 5\n",
      "553th test result 5, the real answer is 5\n",
      "554th test result 5, the real answer is 5\n",
      "555th test result 5, the real answer is 5\n",
      "556th test result 5, the real answer is 5\n",
      "557th test result 5, the real answer is 5\n",
      "558th test result 5, the real answer is 5\n",
      "559th test result 5, the real answer is 5\n",
      "560th test result 5, the real answer is 5\n",
      "561th test result 5, the real answer is 5\n",
      "562th test result 5, the real answer is 5\n",
      "563th test result 5, the real answer is 5\n",
      "564th test result 5, the real answer is 5\n",
      "565th test result 5, the real answer is 5\n",
      "566th test result 5, the real answer is 5\n",
      "567th test result 5, the real answer is 5\n",
      "568th test result 5, the real answer is 5\n",
      "569th test result 5, the real answer is 5\n",
      "570th test result 5, the real answer is 5\n",
      "571th test result 5, the real answer is 5\n",
      "572th test result 5, the real answer is 5\n",
      "573th test result 5, the real answer is 5\n",
      "574th test result 5, the real answer is 5\n",
      "575th test result 5, the real answer is 5\n",
      "576th test result 5, the real answer is 5\n",
      "577th test result 5, the real answer is 5\n",
      "578th test result 5, the real answer is 5\n",
      "579th test result 5, the real answer is 5\n",
      "580th test result 5, the real answer is 5\n",
      "581th test result 5, the real answer is 5\n",
      "582th test result 5, the real answer is 5\n",
      "583th test result 6, the real answer is 6\n",
      "584th test result 6, the real answer is 6\n",
      "585th test result 6, the real answer is 6\n",
      "586th test result 6, the real answer is 6\n",
      "587th test result 6, the real answer is 6\n",
      "588th test result 6, the real answer is 6\n",
      "589th test result 6, the real answer is 6\n",
      "590th test result 6, the real answer is 6\n",
      "591th test result 6, the real answer is 6\n",
      "592th test result 6, the real answer is 6\n",
      "593th test result 6, the real answer is 6\n",
      "594th test result 6, the real answer is 6\n",
      "595th test result 6, the real answer is 6\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "596th test result 6, the real answer is 6\n",
      "597th test result 6, the real answer is 6\n",
      "598th test result 6, the real answer is 6\n",
      "599th test result 6, the real answer is 6\n",
      "600th test result 6, the real answer is 6\n",
      "601th test result 6, the real answer is 6\n",
      "602th test result 6, the real answer is 6\n",
      "603th test result 6, the real answer is 6\n",
      "604th test result 6, the real answer is 6\n",
      "605th test result 6, the real answer is 6\n",
      "606th test result 6, the real answer is 6\n",
      "607th test result 6, the real answer is 6\n",
      "608th test result 6, the real answer is 6\n",
      "609th test result 6, the real answer is 6\n",
      "610th test result 6, the real answer is 6\n",
      "611th test result 6, the real answer is 6\n",
      "612th test result 6, the real answer is 6\n",
      "613th test result 6, the real answer is 6\n",
      "614th test result 6, the real answer is 6\n",
      "615th test result 6, the real answer is 6\n",
      "616th test result 6, the real answer is 6\n",
      "617th test result 6, the real answer is 6\n",
      "618th test result 6, the real answer is 6\n",
      "619th test result 6, the real answer is 6\n",
      "620th test result 6, the real answer is 6\n",
      "621th test result 6, the real answer is 6\n",
      "622th test result 6, the real answer is 6\n",
      "623th test result 6, the real answer is 6\n",
      "624th test result 6, the real answer is 6\n",
      "625th test result 6, the real answer is 6\n",
      "626th test result 6, the real answer is 6\n",
      "627th test result 6, the real answer is 6\n",
      "628th test result 6, the real answer is 6\n",
      "629th test result 6, the real answer is 6\n",
      "630th test result 6, the real answer is 6\n",
      "631th test result 6, the real answer is 6\n",
      "632th test result 6, the real answer is 6\n",
      "633th test result 6, the real answer is 6\n",
      "634th test result 6, the real answer is 6\n",
      "635th test result 6, the real answer is 6\n",
      "636th test result 6, the real answer is 6\n",
      "637th test result 6, the real answer is 6\n",
      "638th test result 6, the real answer is 6\n",
      "639th test result 6, the real answer is 6\n",
      "640th test result 6, the real answer is 6\n",
      "641th test result 6, the real answer is 6\n",
      "642th test result 6, the real answer is 6\n",
      "643th test result 6, the real answer is 6\n",
      "644th test result 6, the real answer is 6\n",
      "645th test result 6, the real answer is 6\n",
      "646th test result 6, the real answer is 6\n",
      "647th test result 6, the real answer is 6\n",
      "648th test result 6, the real answer is 6\n",
      "649th test result 6, the real answer is 6\n",
      "650th test result 6, the real answer is 6\n",
      "651th test result 6, the real answer is 6\n",
      "652th test result 6, the real answer is 6\n",
      "653th test result 6, the real answer is 6\n",
      "654th test result 6, the real answer is 6\n",
      "655th test result 6, the real answer is 6\n",
      "656th test result 6, the real answer is 6\n",
      "657th test result 6, the real answer is 6\n",
      "658th test result 6, the real answer is 6\n",
      "659th test result 6, the real answer is 6\n",
      "660th test result 6, the real answer is 6\n",
      "661th test result 6, the real answer is 6\n",
      "662th test result 6, the real answer is 6\n",
      "663th test result 6, the real answer is 6\n",
      "664th test result 6, the real answer is 6\n",
      "665th test result 6, the real answer is 6\n",
      "666th test result 6, the real answer is 6\n",
      "667th test result 6, the real answer is 6\n",
      "668th test result 6, the real answer is 6\n",
      "669th test result 6, the real answer is 6\n",
      "670th test result 7, the real answer is 7\n",
      "671th test result 7, the real answer is 7\n",
      "672th test result 7, the real answer is 7\n",
      "673th test result 7, the real answer is 7\n",
      "674th test result 7, the real answer is 7\n",
      "675th test result 7, the real answer is 7\n",
      "676th test result 7, the real answer is 7\n",
      "677th test result 7, the real answer is 7\n",
      "678th test result 7, the real answer is 7\n",
      "679th test result 7, the real answer is 7\n",
      "680th test result 7, the real answer is 7\n",
      "681th test result 7, the real answer is 7\n",
      "682th test result 7, the real answer is 7\n",
      "683th test result 7, the real answer is 7\n",
      "684th test result 7, the real answer is 7\n",
      "685th test result 7, the real answer is 7\n",
      "686th test result 7, the real answer is 7\n",
      "687th test result 7, the real answer is 7\n",
      "688th test result 7, the real answer is 7\n",
      "689th test result 7, the real answer is 7\n",
      "690th test result 7, the real answer is 7\n",
      "691th test result 7, the real answer is 7\n",
      "692th test result 7, the real answer is 7\n",
      "693th test result 7, the real answer is 7\n",
      "694th test result 7, the real answer is 7\n",
      "695th test result 7, the real answer is 7\n",
      "696th test result 7, the real answer is 7\n",
      "697th test result 7, the real answer is 7\n",
      "698th test result 7, the real answer is 7\n",
      "699th test result 7, the real answer is 7\n",
      "700th test result 7, the real answer is 7\n",
      "701th test result 7, the real answer is 7\n",
      "702th test result 7, the real answer is 7\n",
      "703th test result 7, the real answer is 7\n",
      "704th test result 7, the real answer is 7\n",
      "705th test result 7, the real answer is 7\n",
      "706th test result 7, the real answer is 7\n",
      "707th test result 7, the real answer is 7\n",
      "708th test result 7, the real answer is 7\n",
      "709th test result 7, the real answer is 7\n",
      "710th test result 7, the real answer is 7\n",
      "711th test result 7, the real answer is 7\n",
      "712th test result 7, the real answer is 7\n",
      "713th test result 7, the real answer is 7\n",
      "714th test result 7, the real answer is 7\n",
      "715th test result 7, the real answer is 7\n",
      "716th test result 7, the real answer is 7\n",
      "717th test result 7, the real answer is 7\n",
      "718th test result 7, the real answer is 7\n",
      "719th test result 7, the real answer is 7\n",
      "720th test result 7, the real answer is 7\n",
      "721th test result 7, the real answer is 7\n",
      "722th test result 7, the real answer is 7\n",
      "723th test result 7, the real answer is 7\n",
      "724th test result 7, the real answer is 7\n",
      "725th test result 7, the real answer is 7\n",
      "726th test result 7, the real answer is 7\n",
      "727th test result 7, the real answer is 7\n",
      "728th test result 7, the real answer is 7\n",
      "729th test result 7, the real answer is 7\n",
      "730th test result 7, the real answer is 7\n",
      "731th test result 7, the real answer is 7\n",
      "732th test result 7, the real answer is 7\n",
      "733th test result 7, the real answer is 7\n",
      "734th test result 7, the real answer is 7\n",
      "735th test result 7, the real answer is 7\n",
      "736th test result 7, the real answer is 7\n",
      "737th test result 7, the real answer is 7\n",
      "738th test result 7, the real answer is 7\n",
      "739th test result 7, the real answer is 7\n",
      "740th test result 7, the real answer is 7\n",
      "741th test result 7, the real answer is 7\n",
      "742th test result 7, the real answer is 7\n",
      "743th test result 7, the real answer is 7\n",
      "744th test result 7, the real answer is 7\n",
      "745th test result 7, the real answer is 7\n",
      "746th test result 7, the real answer is 7\n",
      "747th test result 7, the real answer is 7\n",
      "748th test result 7, the real answer is 7\n",
      "749th test result 7, the real answer is 7\n",
      "750th test result 7, the real answer is 7\n",
      "751th test result 7, the real answer is 7\n",
      "752th test result 7, the real answer is 7\n",
      "753th test result 7, the real answer is 7\n",
      "754th test result 7, the real answer is 7\n",
      "755th test result 7, the real answer is 7\n",
      "756th test result 7, the real answer is 7\n",
      "757th test result 7, the real answer is 7\n",
      "758th test result 7, the real answer is 7\n",
      "759th test result 7, the real answer is 7\n",
      "760th test result 7, the real answer is 7\n",
      "761th test result 7, the real answer is 7\n",
      "762th test result 7, the real answer is 7\n",
      "763th test result 7, the real answer is 7\n",
      "764th test result 7, the real answer is 7\n",
      "765th test result 7, the real answer is 7\n",
      "766th test result 8, the real answer is 8\n",
      "767th test result 8, the real answer is 8\n",
      "768th test result 8, the real answer is 8\n",
      "769th test result 6, the real answer is 8\n",
      "770th test result 8, the real answer is 8\n",
      "771th test result 8, the real answer is 8\n",
      "772th test result 8, the real answer is 8\n",
      "773th test result 8, the real answer is 8\n",
      "774th test result 8, the real answer is 8\n",
      "775th test result 8, the real answer is 8\n",
      "776th test result 8, the real answer is 8\n",
      "777th test result 8, the real answer is 8\n",
      "778th test result 8, the real answer is 8\n",
      "779th test result 8, the real answer is 8\n",
      "780th test result 8, the real answer is 8\n",
      "781th test result 8, the real answer is 8\n",
      "782th test result 3, the real answer is 8\n",
      "783th test result 8, the real answer is 8\n",
      "784th test result 8, the real answer is 8\n",
      "785th test result 8, the real answer is 8\n",
      "786th test result 8, the real answer is 8\n",
      "787th test result 8, the real answer is 8\n",
      "788th test result 8, the real answer is 8\n",
      "789th test result 8, the real answer is 8\n",
      "790th test result 8, the real answer is 8\n",
      "791th test result 8, the real answer is 8\n",
      "792th test result 8, the real answer is 8\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "793th test result 8, the real answer is 8\n",
      "794th test result 8, the real answer is 8\n",
      "795th test result 8, the real answer is 8\n",
      "796th test result 1, the real answer is 8\n",
      "797th test result 8, the real answer is 8\n",
      "798th test result 8, the real answer is 8\n",
      "799th test result 8, the real answer is 8\n",
      "800th test result 8, the real answer is 8\n",
      "801th test result 8, the real answer is 8\n",
      "802th test result 8, the real answer is 8\n",
      "803th test result 8, the real answer is 8\n",
      "804th test result 8, the real answer is 8\n",
      "805th test result 8, the real answer is 8\n",
      "806th test result 1, the real answer is 8\n",
      "807th test result 8, the real answer is 8\n",
      "808th test result 8, the real answer is 8\n",
      "809th test result 8, the real answer is 8\n",
      "810th test result 8, the real answer is 8\n",
      "811th test result 8, the real answer is 8\n",
      "812th test result 8, the real answer is 8\n",
      "813th test result 8, the real answer is 8\n",
      "814th test result 8, the real answer is 8\n",
      "815th test result 8, the real answer is 8\n",
      "816th test result 8, the real answer is 8\n",
      "817th test result 8, the real answer is 8\n",
      "818th test result 8, the real answer is 8\n",
      "819th test result 8, the real answer is 8\n",
      "820th test result 8, the real answer is 8\n",
      "821th test result 8, the real answer is 8\n",
      "822th test result 8, the real answer is 8\n",
      "823th test result 8, the real answer is 8\n",
      "824th test result 8, the real answer is 8\n",
      "825th test result 8, the real answer is 8\n",
      "826th test result 8, the real answer is 8\n",
      "827th test result 8, the real answer is 8\n",
      "828th test result 8, the real answer is 8\n",
      "829th test result 8, the real answer is 8\n",
      "830th test result 8, the real answer is 8\n",
      "831th test result 8, the real answer is 8\n",
      "832th test result 8, the real answer is 8\n",
      "833th test result 8, the real answer is 8\n",
      "834th test result 8, the real answer is 8\n",
      "835th test result 8, the real answer is 8\n",
      "836th test result 8, the real answer is 8\n",
      "837th test result 8, the real answer is 8\n",
      "838th test result 8, the real answer is 8\n",
      "839th test result 8, the real answer is 8\n",
      "840th test result 8, the real answer is 8\n",
      "841th test result 8, the real answer is 8\n",
      "842th test result 8, the real answer is 8\n",
      "843th test result 8, the real answer is 8\n",
      "844th test result 8, the real answer is 8\n",
      "845th test result 8, the real answer is 8\n",
      "846th test result 8, the real answer is 8\n",
      "847th test result 8, the real answer is 8\n",
      "848th test result 8, the real answer is 8\n",
      "849th test result 8, the real answer is 8\n",
      "850th test result 8, the real answer is 8\n",
      "851th test result 8, the real answer is 8\n",
      "852th test result 8, the real answer is 8\n",
      "853th test result 8, the real answer is 8\n",
      "854th test result 8, the real answer is 8\n",
      "855th test result 8, the real answer is 8\n",
      "856th test result 8, the real answer is 8\n",
      "857th test result 9, the real answer is 9\n",
      "858th test result 9, the real answer is 9\n",
      "859th test result 9, the real answer is 9\n",
      "860th test result 9, the real answer is 9\n",
      "861th test result 9, the real answer is 9\n",
      "862th test result 9, the real answer is 9\n",
      "863th test result 1, the real answer is 9\n",
      "864th test result 9, the real answer is 9\n",
      "865th test result 9, the real answer is 9\n",
      "866th test result 9, the real answer is 9\n",
      "867th test result 9, the real answer is 9\n",
      "868th test result 9, the real answer is 9\n",
      "869th test result 9, the real answer is 9\n",
      "870th test result 9, the real answer is 9\n",
      "871th test result 9, the real answer is 9\n",
      "872th test result 9, the real answer is 9\n",
      "873th test result 9, the real answer is 9\n",
      "874th test result 9, the real answer is 9\n",
      "875th test result 9, the real answer is 9\n",
      "876th test result 9, the real answer is 9\n",
      "877th test result 9, the real answer is 9\n",
      "878th test result 9, the real answer is 9\n",
      "879th test result 9, the real answer is 9\n",
      "880th test result 9, the real answer is 9\n",
      "881th test result 9, the real answer is 9\n",
      "882th test result 9, the real answer is 9\n",
      "883th test result 9, the real answer is 9\n",
      "884th test result 9, the real answer is 9\n",
      "885th test result 9, the real answer is 9\n",
      "886th test result 9, the real answer is 9\n",
      "887th test result 9, the real answer is 9\n",
      "888th test result 9, the real answer is 9\n",
      "889th test result 9, the real answer is 9\n",
      "890th test result 9, the real answer is 9\n",
      "891th test result 9, the real answer is 9\n",
      "892th test result 9, the real answer is 9\n",
      "893th test result 9, the real answer is 9\n",
      "894th test result 9, the real answer is 9\n",
      "895th test result 9, the real answer is 9\n",
      "896th test result 9, the real answer is 9\n",
      "897th test result 9, the real answer is 9\n",
      "898th test result 9, the real answer is 9\n",
      "899th test result 9, the real answer is 9\n",
      "900th test result 9, the real answer is 9\n",
      "901th test result 9, the real answer is 9\n",
      "902th test result 9, the real answer is 9\n",
      "903th test result 9, the real answer is 9\n",
      "904th test result 9, the real answer is 9\n",
      "905th test result 9, the real answer is 9\n",
      "906th test result 9, the real answer is 9\n",
      "907th test result 9, the real answer is 9\n",
      "908th test result 9, the real answer is 9\n",
      "909th test result 9, the real answer is 9\n",
      "910th test result 9, the real answer is 9\n",
      "911th test result 9, the real answer is 9\n",
      "912th test result 9, the real answer is 9\n",
      "913th test result 9, the real answer is 9\n",
      "914th test result 7, the real answer is 9\n",
      "915th test result 9, the real answer is 9\n",
      "916th test result 9, the real answer is 9\n",
      "917th test result 9, the real answer is 9\n",
      "918th test result 9, the real answer is 9\n",
      "919th test result 9, the real answer is 9\n",
      "920th test result 9, the real answer is 9\n",
      "921th test result 9, the real answer is 9\n",
      "922th test result 9, the real answer is 9\n",
      "923th test result 9, the real answer is 9\n",
      "924th test result 9, the real answer is 9\n",
      "925th test result 9, the real answer is 9\n",
      "926th test result 9, the real answer is 9\n",
      "927th test result 9, the real answer is 9\n",
      "928th test result 9, the real answer is 9\n",
      "929th test result 9, the real answer is 9\n",
      "930th test result 9, the real answer is 9\n",
      "931th test result 9, the real answer is 9\n",
      "932th test result 9, the real answer is 9\n",
      "933th test result 9, the real answer is 9\n",
      "934th test result 9, the real answer is 9\n",
      "935th test result 9, the real answer is 9\n",
      "936th test result 9, the real answer is 9\n",
      "937th test result 9, the real answer is 9\n",
      "938th test result 9, the real answer is 9\n",
      "939th test result 9, the real answer is 9\n",
      "940th test result 9, the real answer is 9\n",
      "941th test result 9, the real answer is 9\n",
      "942th test result 9, the real answer is 9\n",
      "943th test result 9, the real answer is 9\n",
      "944th test result 9, the real answer is 9\n",
      "945th test result 9, the real answer is 9\n",
      "the total error rate is 0.010571\n"
     ]
    }
   ],
   "source": [
    "from os import listdir\n",
    "\n",
    "def handwritingClassTest():\n",
    "    hwLabels=[]\n",
    "    trainingFileList=listdir('trainingDigits')\n",
    "    m=len(trainingFileList)\n",
    "    trainingMat=zeros((m,1024))\n",
    "    for i in range(m):\n",
    "        fileNameStr=trainingFileList[i]\n",
    "        fileStr=fileNameStr.split('.')[0]\n",
    "        classNumStr=int(fileStr.split('_')[0])\n",
    "        hwLabels.append(classNumStr)\n",
    "        trainingMat[i,:]=img2vec('trainingDigits/%s' % fileNameStr)\n",
    "    testFileList=listdir('testDigits')\n",
    "    errorCount=0.0\n",
    "    mTest=len(testFileList)\n",
    "    for i in range(mTest):\n",
    "        fileNameStr=testFileList[i]\n",
    "        fileStr=fileNameStr.split('.')[0]\n",
    "        classNumStr=int(fileStr.split('_')[0])\n",
    "        vecUnderTest=img2vec('testDigits/%s' % fileNameStr)\n",
    "        classifierResult=classify0(vecUnderTest,\n",
    "                                  trainingMat,hwLabels,3)\n",
    "        print(\"%dth test result %s, the real answer is %s\" %\\\n",
    "              (i,classifierResult,classNumStr))\n",
    "        if(classifierResult!=classNumStr):\n",
    "            errorCount+=1.0\n",
    "    print (\"the total error rate is %f\" % (errorCount/float(mTest))) \n",
    "handwritingClassTest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
